﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Dto;
using ProTrack.IServices.Integrated;
using System.Linq;
using System;
using System.Threading.Tasks;
using ProTrack.Api.Helper;
using ProTrack.Entity;
using System.Drawing.Printing;
using System.Xml.Linq;
using ProTrack.Utility;
using ProTrack.Report;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 供应商服务接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class VendorController : ControllerBase
    {
        public VendorController(IVendorServices services,LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }

        public IVendorServices Services { get; }
        public LogHelper LogHelper { get; }



        /// <summary>
        /// 获取所有供应商
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();

            try
            {
                var list = await Services.GetAllAsync();
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";

                //写日志
                await LogHelper.WriteErrorLog("供应商-获取所有", ex, HttpContext);
            }

            return Ok(status);
        }

        /// <summary>
        /// 分页获取供应商数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="VendorCode"></param>
        /// <param name="VendorName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get([FromQuery]int PageIndex, [FromQuery] int PageSize, [FromQuery] string VendorCode, [FromQuery] string VendorName, [FromQuery] string VendorNick, [FromQuery] string VendorEn, [FromQuery] string Tel, [FromQuery] string VendorLevel, [FromQuery] string EnableFlag)
        {
            var status = BaseStatus.CreateInstance();

            try
            {
                VendorCode = VendorCode ?? "";
                VendorName = VendorName ?? "";
                VendorNick = VendorNick ?? "";
                VendorEn = VendorEn ?? "";
                Tel = Tel ?? "";
                VendorLevel = VendorLevel ?? "";
                EnableFlag = EnableFlag ?? "";


                var list = await Services.GetAsync(PageIndex, PageSize, VendorCode, VendorName, VendorNick, VendorEn, Tel, VendorLevel, EnableFlag);
                status.Data = list;
                status.Count = await Services.GetCountAsync(VendorCode, VendorName, VendorNick, VendorEn, Tel, VendorLevel, EnableFlag);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";

                //写日志
                await LogHelper.WriteErrorLog("供应商-分页获取", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 新增供应商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] Vendor model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.CreateTime = StringHelper.GetTimeNow();
                model.Creator = User.Identity.Name;

                var isSuccess = await Services.AddAsync(model);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("供应商-新增", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 修改供应商数据
        /// </summary>
        /// <param name="model">规格模型</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] Vendor model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model);
                if (isSuccess)
                {
                    status.Msg = "修改成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("供应商-修改", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 批量删除供应商数据
        /// </summary>
        /// <param name="Ids">Id数组</param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("供应商-批量删除", ex, HttpContext);
            }

            return Ok(status);
        }

        /// <summary>
        /// 删除供应商数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("供应商-删除", ex, HttpContext);
            }

            return Ok(status);
        }





        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet("Export")]
        public async Task<IActionResult> Export()
        {
            try
            {

                var list = await Services.GetAllAsync();
                var TitleArray = new ReportDomain[] {
                    new ReportDomain{ Title="序号",Fileld="No"},
                    new ReportDomain{ Title="供应商编码",Fileld="VendorCode"},
                    new ReportDomain{ Title="供应商名称",Fileld="VendorName"},
                    new ReportDomain{ Title="供应商简称",Fileld="VendorNick"},
                    new ReportDomain{ Title="供应商等级",Fileld="VendorLevel"},
                    new ReportDomain{ Title="供应商评分",Fileld="VendorScore"},
                    new ReportDomain{ Title="供应商电话",Fileld="Tel"},
                    new ReportDomain{ Title="备注",Fileld="Remark"},

                };
                var fileArray = BaseReport.Export(list, "供应商信息", TitleArray);

                return File(new System.IO.MemoryStream(fileArray), "application/octet-stream", "供应商信息.xls");
            }
            catch (Exception ex)
            {

                //此处应当日志记录
                await LogHelper.WriteErrorLog("供应商信息-导出", ex, HttpContext);
                return new JsonResult(BaseStatus.CreateInstance(-1, "导出失败，服务器异常请稍后再试！"));
            }

        }

    }
}
